package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.model.DeviceState;
import com.kust.smarthome.web.model.Door;
import com.kust.smarthome.web.model.Notice;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IDeviceStateService;
import com.kust.smarthome.web.service.IDoorService;
import com.kust.smarthome.web.service.INoticeService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/door")
@Api(value = "门相关操作", tags = {"device-tag"})
public class DoorController {

    @Resource
    private IDoorService doorService;

    @Resource
    private IDeviceStateService deviceStateService;

    @Resource
    private IUserService userService;

    /**
     * 通过对应用户信息查询用户家庭门数据
     * @param principal SpringSecurity自带属性,登录之后就有值
     * @return 返回用户智能们信息
     */
    @ApiOperation(value = "获取门信息", notes = "对应用户信息获取们信息")
    @GetMapping("")
    public List<Door> list(Principal principal) {

        User u = userService.getByUsername(principal.getName());

        return doorService.getByUserId(u.getId());
    }

    @GetMapping("/getById")
    @ApiOperation(value = "getById", notes = "通过id获取对应设备")
    public Door getById(Integer id) {
        return doorService.getById(id);
    }

    @GetMapping("/add")
    @Transactional
    @ApiOperation(value = "添加门", notes = "用户提供门id添加门信息")
    public List<Door> addDoor(Principal principal, DeviceVo deviceVo){
        User u = userService.getByUsername(principal.getName());

        return doorService.add(u.getId(),deviceVo);
    }

    @GetMapping("/deleteById")
    @Transactional
    @ApiOperation(value = "删除门", notes = "用户提供门id删除门")
    public boolean deleteById(Integer id) {
        Door door = doorService.getById(id);
        door.setUserId(-1);
        boolean d = doorService.updateById(door);
        QueryWrapper<DeviceState> query = new QueryWrapper<>();
        query.eq("device_id",id);
        query.eq("type_id",3);
        deviceStateService.remove(query);
        return d;
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "更新门密码", notes = "用户提供新密码以及门id更新门密码")
    public String updatePassword(Principal principal, Integer id, String password) {
        User u = userService.getByUsername(principal.getName());

        return doorService.updatePassword(u.getId(),id,password);

    }

    @Resource
    private INoticeService noticeService;

    @GetMapping("/warning")
    @ApiOperation(value = "门警告", notes = "一定时间内没有关门自动发出警告请求,需要门自行做判断(前端)")
    public void warning(Integer id) {

        Door door = doorService.getById(id);

        Notice notice = new Notice()
                .setUserId(door.getUserId())
                .setTypeId(3)
                .setDeviceId(id)
                .setContent("门未关时间过长")
                .setCreated(LocalDateTime.now())
                .setState(0);

        noticeService.save(notice);
    }
}
